<?php defined('SYSPATH') OR die('No direct access allowed.');
 
class frontmenu_Core { 
	
	public static function get_nested_subpages($page, $selected=NULL, $numsteps=0, $step=0, $urlpreset=""){
		$r='';
		
		$openones=$selected->get_openones();
		if($numsteps>0){
			if($step>=$numsteps){
				return;
			}
		}
		
		$r.= '<ul class="indented_menu">';
		foreach($page->where('visible',1)->children->as_array() as $child){
			$r.= '<li class="mp">';
			
			$linkclass='';
			if($child->id==$selected->id){
				$linkclass='selected';
			}
			
			$url=$urlpreset.'/'.$child->shortname;
			$r.= html::anchor($url, $child->title , array('class'=>$linkclass));
			
			
			if(in_array($child->id, $openones)){
				$r.= frontmenu::get_nested_subpages($child,$selected,  $numsteps, $step+1, $urlpreset);
			}
			$r.= '</li>';	
		}
		$r.='</ul>';
		
		return $r;
	}
	
	public static function get_listed_subpages($page, $selected=NULL, $numsteps=0, $step=0){
		$r='';
		$openones=array_reverse($selected->get_openones());
		
		if($numsteps>0){
			if($step>=$numsteps){
				return;
			}
		}
		
		$count=0;
	
		foreach($openones as $openone){
			$page=ORM::factory('page',$openone);
			$childarray=$page->where('visible',1)->children->as_array();
			if(count($childarray)>0){
				if($count==count($openones)-1){
					$ulclass='open';
				}else{
					$ulclass='';
				}
				$r.= '<ul class="listed_menu '.$ulclass.'">';							
				foreach($childarray as $child){
					$liclass='';
					$isopen=(in_array($child->id, $openones));
					
					if($isopen){
						$liclass='open';
					}
					$r.= '<li class="mp '.$liclass.'">';
					
					$linkclass='';
					if($isopen){
						$linkclass='selected';
					}
					$r.= html::anchor($child->shortname, $child->get_meta_object()->menutitle , array('class'=>$linkclass));
					$r.= '</li>';	
				}
				$r.='</ul>';
				
			}
			$count++;
		}
		
		
		return $r;
	}
	
	public static function get_prev_next_id($startid=NULL, $contents=NULL){
		$all=$contents;
		$count=0;
		$chosenind=0;
		foreach($all as $elm){
			if($elm->id==$startid){
				$chosenind=$count;
				break;
			}	
			$count++;
		}
		$nextindex=$chosenind+1;
		$previndex=$chosenind-1;
		
		$nextid='';
		$previd='';
		if($nextindex<count($all)){
			$nextid=$all[$nextindex]->shortname;
		}
		if($previndex>=0){
			$previd=$all[$previndex]->shortname;
		}
	
	return array($previd, $nextid);
	}	
	
	
	
	public static function get_lang_navi($langarray, $langselected=NULL, $function=NULL, $goafter){
		$r= '<ul>';
		foreach($langarray as $lang=>$langname){
			$class='';
			if($lang==$langselected){
				$class='selected';
			}			
			$r.= '<li>';
			$r.= html::anchor($function.$lang.'/'.$goafter, $langname, array('class'=>$class));
			$r.='</li>';
		}
		$r.='</ul>';
		return $r;
	}
	
	
	
}